import { execSync as exec } from "child_process";

export class Deployer {
    constructor(private dataPath: string, private nodeIps: string[], private password: string, private keyPath: string) {}

    async run() {
        for (let i = 0; i < this.nodeIps.length; i++) {
            exec(`ssh root@${this.nodeIps[i]} -i ${this.keyPath} 'apt update; apt install -y docker.io'`);
            exec(`ssh root@${this.nodeIps[i]} -i ${this.keyPath} 'docker kill validator | true'`);
            exec(`ssh root@${this.nodeIps[i]} -i ${this.keyPath} 'docker rm validator | true'`);
            exec(`ssh root@${this.nodeIps[i]} -i ${this.keyPath} '/bin/bash -c "rm -rf /root/.exrpd | true"'`);
            exec(`ssh root@${this.nodeIps[i]} -i ${this.keyPath} '/bin/bash -c "mkdir /root/.exrpd"'`);
            exec(`scp -r -i ${this.keyPath} ./data/validator-${i}/* root@${this.nodeIps[i]}:/root/.exrpd `);
            exec(
                `ssh root@${this.nodeIps[i]} -i ${this.keyPath} 'docker run -d -p 8545:8545 -p 26656:26656 -v /root:/root --name validator peersyst/xrp-evm-client:poa sh -c '"'"'yes ${this.password} | exrpd start --keyring-backend file --pruning=nothing --evm.tracer=json --log_level info --json-rpc.api eth,txpool,personal,net,debug,web3,miner --api.enable'"'"' '`,
            );
        }
    }
}
